package com.kaili.basic.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.common.entity.BaseDepartment;
import com.kaili.basic.mapper.BaseDepartmentMapper;
import com.kaili.basic.service.BaseDepartmentService;
import com.kaili.common.entity.BaseDepartmentUpdateVO;
import com.kaili.common.util.LoginDetailUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class BaseDepartmentServiceImpl implements BaseDepartmentService {

    @Autowired
    BaseDepartmentMapper baseDepartmentMapper;

    public int addDep(BaseDepartment baseDepartment) {
        baseDepartment.setEnabled(true);
        if (baseDepartment.getParentId() != null){
            baseDepartment.setParent(false);
        }else{
            baseDepartment.setParent(true);
        }
        baseDepartment.setCreateBy(LoginDetailUtils.getCurrentEmployeeId());
        baseDepartment.setCreateTime(new Date());
        baseDepartment.setRemark("/");
        baseDepartment.setDepPath("/");
//        baseDepartmentMapper.addDep(baseDepartment);
        return baseDepartmentMapper.addDep(baseDepartment);
//        return baseDepartment.getResult();
    }

    public int deleteDep(Long did) {
//        BaseDepartment baseDepartment = new BaseDepartment();
//        baseDepartment.setId(did);
//        baseDepartmentMapper.deleteDep(did);
        return baseDepartmentMapper.deleteDep(did);
    }

    public List<BaseDepartment> getDepByPid(Long pid) {
        return baseDepartmentMapper.getDepByPid(pid);
    }

    public List<BaseDepartment> getAllDeps() {
        return baseDepartmentMapper.getAllDeps();
    }

    public Page<BaseDepartment> getPageDeps(Page<BaseDepartment> page, BaseDepartment baseDepartment){
        return page.setRecords(baseDepartmentMapper.getPageDeps(page,baseDepartment));
    }


    public int modifyDep(BaseDepartmentUpdateVO baseDepartment){
        baseDepartment.setModifyBy(LoginDetailUtils.getCurrentEmployeeId());
        baseDepartment.setModifyTime(new Date());
        return baseDepartmentMapper.modifyDep(baseDepartment);
    }

}
